<template>
  <div class="content">
    <el-form label-width="auto" title="配置客服信息" style="max-width: 600px">
      <el-form-item label="微信二维码(客服一)">
        <el-upload class="avatar-uploader" action="http://120.79.170.227:5000/api/public/upload" :show-file-list="false"
          :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
          <img v-if="imageUrl_one" :src="imageUrl_one" class="avatar" />
          <el-icon v-else class="avatar-uploader-icon">
            <Plus />
          </el-icon>
        </el-upload>
      </el-form-item>
      <el-form-item label="手机号码(客服一)">
        <el-input v-model="phone_one" placeholder="请输入客服一的手机号码" />
      </el-form-item>
      <el-form-item label="微信二维码(客服二)">
        <el-upload class="avatar-uploader" action="http://120.79.170.227:5000/api/public/upload" :show-file-list="false"
          :on-success="handleAvatarSuccess_two" :before-upload="beforeAvatarUpload">
          <img v-if="imageUrl_two" :src="imageUrl_two" class="avatar" />
          <el-icon v-else class="avatar-uploader-icon">
            <Plus />
          </el-icon>
        </el-upload>
      </el-form-item>
      <el-form-item label="手机号码(客服二)">
        <el-input v-model="phone_two" placeholder="请输入客服二的手机号码" />
      </el-form-item>
    </el-form>
    <el-button type="primary" size="large" @click="handleService">保存配置</el-button>
  </div>
</template>

<script lang="ts" setup>
  import { ref } from 'vue';
  import { ElMessage } from 'element-plus'
  import { Delete, Plus, UploadFilled } from '@element-plus/icons-vue';
  import { service, getSystemConfig } from '/@/api/newPagesApi';
  import type { UploadProps } from 'element-plus'

  const $baseConfirm = inject<any>('$baseConfirm')
  const $baseMessage = inject<any>('$baseMessage')
  const imageUrl_one = ref('')
  const imageUrl_two = ref('')
  const id = ref('')
  const phone_one = ref('')
  const phone_two = ref('')

  const handleAvatarSuccess : UploadProps['onSuccess'] = (
    response,
    uploadFile
  ) => {
    imageUrl_one.value = response.data.url;
  }

  const handleAvatarSuccess_two : UploadProps['onSuccess'] = (
    response,
    uploadFile
  ) => {
    imageUrl_two.value = response.data.url;
  }

  const beforeAvatarUpload : UploadProps['beforeUpload'] = (rawFile) => {
    if (rawFile.size / 1024 / 1024 > 2) {
      ElMessage.error('图片大小不能超过2MB')
      return false
    }

    return true
  }

  //获取客服系统配置
  const fetchData = async () => {
    try {
      const { data } = await getSystemConfig({
        type: 'server'
      });

      if (data.length === 0) return false;
      imageUrl_one.value = data[0].partnerServer.qrcode1;
      imageUrl_two.value = data[0].partnerServer.qrcode2;
      phone_one.value = data[0].partnerServer.phone1;
      phone_two.value = data[0].partnerServer.phone2;
      id.value = data[0]._id;
    } catch (err) {
      console.log(err);
    }
  }

  //轮播图上传
  const handleService = async () => {
    try {
      const { message } : any = await service({
        qrcode_link1: imageUrl_one.value,
        qrcode_link2: imageUrl_two.value,
        server_phone1: phone_one.value,
        server_phone2: phone_two.value,
        server_id: id.value || ''
      });

      $baseMessage(message, 'success', 'hey')
    } catch (err) {
      console.log(err);
    }
  }

  onBeforeMount(() => {
    fetchData();
  })
</script>

<style scoped>
  .content {
    width: 100%;
    background-color: #fff;
    padding: 10px 10px 10px 10px;
  }

  .avatar-uploader .el-upload {
    border: 1px dashed var(--el-border-color);
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    transition: var(--el-transition-duration-fast);
  }

  .avatar-uploader .el-upload:hover {
    border-color: var(--el-color-primary);
  }

  .el-icon.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    text-align: center;
  }
</style>
